import { Component, OnInit, OnDestroy } from '@angular/core';
import { TitleService, _HttpClient, SettingsService } from '@delon/theme';
import { NzMessageService, UploadChangeParam, UploadXHRArgs } from 'ng-zorro-antd';
import { HttpRequest, HttpEvent, HttpEventType, HttpResponse, HttpClient } from '@angular/common/http';
import { Subject } from 'rxjs';

@Component({
  selector: 'app-product-image',
  templateUrl: './product-image.component.html',
  styleUrls: ['./product-image.component.less'],
})
export class ProductImageComponent implements OnInit, OnDestroy {
  visible = false;
  extraParam = {};
  url = `/cf/prod/sys-prod/uploadPics`;
  fileListTmp = [];
  fileListTmp2 = [];
  p = '';
  auth_batch_btn = false;
  checked = false;
  type = 'all';
  // 表示是否有进行上传操作
  up = false;
  $sign = new Subject<string>();
  // 是否有zip上传任务在执行
  ziping = false;
  zipdesc = '';
  parcent = 0;
  private interval$: any;

  constructor(
    titSrv: TitleService,
    public http: _HttpClient,
    public hp: HttpClient,
    private msg: NzMessageService,
    private settings: SettingsService,
  ) {
    titSrv.setTitle('产品图片管理');
  }

  ngOnInit() {
    this.auth_batch_btn = this.settings.user.permissions.sys_prod_del_pic;
  }

  ngOnDestroy() {
    this.$sign.unsubscribe();
    this.closeStatusTest();
  }

  /**
   * 开启状态检查
   */
  startStatusTest() {
    this.interval$ = setInterval(() => {
      this.testStatus();
    }, 1000);
  }
  /**
   * 关闭状态检查
   */
  closeStatusTest() {
    if (this.interval$) {
      clearInterval(this.interval$);
    }
  }

  open() {
    this.visible = true;
    this.up = false;
    this.startStatusTest();
  }

  close() {
    this.visible = false;
    this.fileListTmp = [];
    this.fileListTmp2 = [];
    if (this.up) {
      this.up = false;
    }
    this.$sign.next('reload');
    this.closeStatusTest();
  }

  handleChange(t: UploadChangeParam): void {
    const status = t.file.status;
    this.up = true;
  }

  beforeUpload = (file: File) => {
    const isJPG = file.type === 'image/jpeg';
    const isPNG = file.type === 'image/png';
    if (!isJPG && !isPNG) {
      this.msg.warning(`不支持的文件类型: ${file.type}`);
    }
    return isJPG || isPNG;
  };
  beforeUpload2 = (file: File) => {
    const isZip = file.type === 'application/zip';
    if (!isZip) {
      this.msg.warning(`不支持的文件类型: ${file.type}`);
    }
    return isZip;
  };

  toSearch(val) {
    this.p = val;
  }

  typeChange(val) {
    this.type = val ? 'nopic' : 'all';
  }

  testStatus() {
    this.http.get(`/cf/prod/sys-prod/testStatus`).subscribe(res => {
      if (res.status === 200) {
        if (res.data) {
          this.ziping = true;
          if (res.data === 'ZIP_UPLOAD_PENDING') {
            this.zipdesc = '正在传输到分布式文件系统';
          } else if (res.data === 'ZIP_UPLOAD_UPDATE') {
            this.zipdesc = '准备同步';
          } else {
            if (res.data.endsWith('P')) {
              this.zipdesc = '图片上传中...';
              res.data = res.data.replace('P', '');
            } else {
              this.zipdesc = '图片同步中...';
              res.data = res.data.replace('U', '');
            }

            this.parcent = +res.data;
          }
        } else {
          this.ziping = false;
        }
      }
    });
  }
}
